package com.qf.exception;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;

/**
 * 自定义异常解析器   全局统一异常处理器
 * 注解式    就近原则  ：  离抛出的异常最近的异常
 *
 *        FileNotFoundException
 *
 *
 *        RuntimeException
 *
 *        Exception
 * @author lixu
 */
@ControllerAdvice
public class CustomAnnHandlerExceptionResolver{


    /**
     * 处理异常  大异常
     */
    @ExceptionHandler(Exception.class)
    public String handlerException(Exception ex, Model model){
        System.out.println("处理异常:" + ex.getMessage());
        model.addAttribute("error","........");
        return "index";
    }

    /**
     * 处理异常  算术异常
     */
    @ExceptionHandler(ArithmeticException.class)
    public String handlerArithmeticException(ArithmeticException aex, Model model){
        System.out.println("处理异常:" + aex.getMessage());
        model.addAttribute("error","........");
        return "index";
    }
    /**
     * 处理异常   File未发现异常
     */
    @ExceptionHandler(FileNotFoundException.class)
    public String handlerFileNotFoundException(FileNotFoundException aex, Model model){
        System.out.println("处理异常:" + aex.getMessage());
        model.addAttribute("error","........");
        return "index";
    }
}
